package za.um.td.state.client.handler;

import java.util.logging.Logger;

import org.lex.input.StrategicHandler;
import org.lex.input.effects.HeightMap;

import za.um.td.entity.manager.LevelEntityManager;
import za.um.td.entity.mouse.MouseEntity;
import za.um.td.entity.mouse.Placer.PlacerHandler;
import za.um.td.input.action.ExitLevelAction;
import za.um.td.state.client.ClientStateNode;

import com.jme.input.InputHandler;
import com.jme.input.KeyBindingManager;
import com.jme.input.KeyInput;
import com.jme.math.Vector3f;

public class LevelHandler extends InputHandler {
	private static final Logger LOG = Logger.getLogger(LevelHandler.class
			.getName());
	
	private StrategicHandler strategicHandler;
	private PlacerHandler placerHandler;
	
	private MouseEntity mouse;

	public LevelHandler() {
		setKeyBindings();
		setActions();
		
		mouse = ClientStateNode.getInstance().getMouse();
		
		HeightMap heightMap = new HeightMap() {
			public float getHeightAt(Vector3f location) {
				float height = LevelEntityManager.getInstance().getTerrain().getHeight(location);
				if (Float.isNaN(height)) height = 0;
				return height;
			}
		};
		mouse.initStrategicHandler(heightMap);
		
		strategicHandler = mouse.getStrategicHandler();
		placerHandler = new PlacerHandler();
		
		addToAttachedHandlers(strategicHandler);
		mouse.getMouseManager().registerWithInputHandler(mouse.getStrategicHandler());
		addToAttachedHandlers(placerHandler);
	}

	private void setKeyBindings() {
		KeyBindingManager.getKeyBindingManager().set("exit",
				KeyInput.KEY_ESCAPE);
	}

	private void setActions() {
		addAction(new ExitLevelAction(), "exit", false);
	}

	public void update(float tpf) {
		
		super.update(tpf);
	}
}
